﻿//卫星数据集
var aroundPoint = new Array();
var count = 0;
var speed = 1;
var pointstep = 100;
var sunTime = 1;
var realPoint = null;
var nowpoint = 1;
var print = 0;
var sid = "";
var isStartPoint = false;
var isRun = true;
var isgoogelmaprun = 0;
var shaptimes = 0;
var shaptimelong = 3;
var gotime = 0;
var addspeedshaptimes = 0;

//初始化页面
//更换卫星

function checkbrowsversion(){
	if(navigator.appoversion){
		if(parseFloat(navigator.appoversion.split("MSIE")[1]) == 6){
				document.getElementById("demo1").innerHTML = "";
				document.getElementById("demo2").innerHTML = "";
		}
	}

}
function addSatellite(){
	checkbrowsversion();
	document.getElementById("calculates").style.display = 'block';
	document.getElementById("satellitenamediv").style.display = 'none';
	var str = document.getElementById("satellite").value;
	sid = str.substring(0,str.indexOf(":"));
	document.getElementById("satelliteHeight").innerHTML = str.substring(str.indexOf(":")+1,str.indexOf("#"));
	sname = str.substring(str.indexOf("#")+1);
	document.getElementById("satellitenamediv").innerHTML=sname;
	document.getElementById("contener").innerHTML = "";
	loadData(sid)
}
//加载数据
function loadData(id){
    $.get("servlet/ShortPointServlet?action=list",{satelliteId: id},function(data){
		if(data == 0){
			alert("当前卫星没有可用轨迹数据");
		}else{
			eval(data);
			count=0;
			speed = 1;
			sunTime = 1;
			realPoint = null;
			shaptimes = 0;
			gotime = 0;
			addspeedshaptimes = 0;
			print = 0;
			isStartPoint = false;
			isRun = true;
			document.getElementById("speedshow").innerHTML=speed;
			document.getElementById("isintimeruning").innerHTML="实时状态";
			document.getElementById("speedshowsatellite").innerHTML = sname;
			initpage();
		}
	}); 		  
}

function initpage(){
	//记录当前时间  
	var dateStr = formatDate(new Date());
	//初始化光照
	//初始化所有点，默认轨迹设为红色
	var tempinitcount = 0;
	var idcount = 1;
	gotime = 0.004166667*points.length;	
	for(var ij=0;ij<shaptimelong;ij++){
		for(var pi=0;pi<points.length;pi++){
			var point = points[pi];
			var time = points[pi][0];
			if(dateStr == time){
				isStartPoint = true;
			}
			if(tempinitcount != pointstep){
				tempinitcount++;
				continue;
			}
			if(isStartPoint){
				count = pi;
				realPoint = pi;
				nowpoint = idcount;
				isStartPoint = false;
				shaptimes = ij;
			}
			tempinitcount = 0;
			var xs = points[pi][1];
			var ys = points[pi][2];
			var x = xs.substring(0,xs.indexOf("."));
			if(x == "-0"){
				x = 0;
			}
			var y = ys.substring(0,ys.indexOf("."));
			if(y == "-0"){
				y = 0;
			}
			x = w + (parseInt(x)+180)*2;	
			y = h - (parseInt(y)+90)*2;
			document.getElementById("contener").innerHTML += "<div id="+idcount+" class=pointscss style='top: "+y+"px;left: "+x+"px;'></div>";		
			idcount ++;
		}
		//偏移
		if(shaptimelong-ij != 1){
			for(var pii=0;pii<points.length;pii++){
				var point = points[pii];
				var time = points[pii][0];
				var x = points[pii][1];
				x = parseFloat(x) - gotime;
				if(x < -180){
					x += 360   
				}
				//****
				//****增加时间 time + points.length(s)
				//****
				points[pii][0] = addtime(time, points.length);
				points[pii][1] = x +"";
			}
		}
	}
	//定位到当前时间
	for(var pi=0;pi<points.length;pi++){
		var point = points[pi];
		var time = points[pi][0];
		var x = points[pi][1];
		for(var ii=0;ii<shaptimelong-shaptimes-1;ii++){
			x = parseFloat(x) + gotime;
			if(x > 180){
				x += -360   
			}
		}
		points[pi][1] = x +"";
		//****
		//****减去时间 time - shaptimes*points.length(s)
		//****

		points[pi][0] = addtime(time, -(shaptimelong-shaptimes-1)*points.length);

	}	
	//启动卫星实时运行
	setLightArea();
	var y = document.getElementById(nowpoint).style.top + 4;
	var x = document.getElementById(nowpoint).style.left + 4;
	document.getElementById("contener").innerHTML += "<div id=\"nowpoint\" class=pointscss style=\"background-image: url('images/map/mianpointb.gif');z-index:200;height:8px;width:8px;top: "+y+"px;left: "+x+"px;\"></div>";		

	//modifyTime();
	//初始化卫星点
	var x = points[count][1].substring(0,points[count][1].indexOf("."));
	var y = points[count][2].substring(0,points[count][2].indexOf("."));
	if(x == "-0"){
		x = 0;
	}	
	if(y == "-0"){
		y = 0;
	}
	printAroundPionts(x,y)
	document.getElementById("satellitenamediv").style.display = 'block';
	document.getElementById("calculates").style.display = 'none';
	run(); 
}	

//光照初始化
function setLightArea(){
	var tab=document.getElementById("demo");
	var time = new Date();
	var baseSeconds = 72960;
	var totalSeconds = time.getHours()*3600 + time.getMinutes()*60;
	var step = (totalSeconds-baseSeconds)/120;
	if(step<0){
		step += 720
	}
	step += ""
	if(step.indexOf(".")!=-1){
		step = step.substring(0,step.indexOf("."));
	}
	tab.scrollLeft = step;
}


//卫星实时运行
function run(){
	if(timeOut){
		Window.clearTimeout(timeOut);
	}
	if(points.length < speed+count){
		isRun = false;
	}
	if(!isRun){
		//偏移
		if(speed != 1){
			addspeedshaptimes += 1;
		}
		for(var pii=0;pii<points.length;pii++){
			var point = points[pii];
			var time = points[pii][0];
			var x = points[pii][1];
			x = parseFloat(x) - gotime;
			if(x < -180){
				x += 360   
			}
			//****
			//****增加时间 time + points.length(s)
			//****
			points[pii][0] = addtime(time, points.length);
			points[pii][1] = x +"";
		}
		//loadData(sid);
		isRun = true;
		count = 0;
	}
	for(var z=count;z<=speed+count;z++){
		printPoint(z);
	}
	if(speed == 1){
		document.getElementById("timetext").innerHTML= formatDate(new Date());
	}else{
		document.getElementById("timetext").innerHTML= points[count][0];
	}
	if(points[count][1].length >=8){
		document.getElementById("xpointshow").innerHTML= points[count][1].substring(0,8);
	}else{
		document.getElementById("xpointshow").innerHTML= points[count][1];
	}

	document.getElementById("ypointshow").innerHTML= points[count][2];
	if(isgoogelmaprun == 5){
//**************google屏蔽**************************
		setgooglemap(points[count][1],points[count][2]);
		isgoogelmaprun = 0;
	}else{
		isgoogelmaprun +=1 ;
	}
	runmarquee();
	count += speed;
	sunTime += speed;
	realPoint+=1;
	if(realPoint >= points.length){
		realPoint = 0;
		shaptimes += 1;
	}
   	var timeOut = setTimeout("run()",1000);
}

function setgooglemap(_x,_y){
	window.googlemap.window.x = _y;
	window.googlemap.window.y = _x;
	window.googlemap.window.initialize();
}
function printPoint(z){
	//取得当前时刻对应该点
	var pointObject = points[z];
	
	//更新卫星信息
	var x = pointObject[1].substring(0,pointObject[1].indexOf("."));
	var y = pointObject[2].substring(0,pointObject[2].indexOf("."));
	if(x == "-0"){
		x = 0;
	}	
	if(y == "-0"){
		y = 0;
	}
	var point =	y+"_"+x;
	//卫星状态显示，默认每100秒采样一次
	
	if(print == pointstep){
		pianyipoint(z);
		printAroundPionts(x,y);
		//document.getElementById(point).bgColor="#00ff00";
		print = 0;
	}else{
		print += 1;
	}
}
function pianyipoint(pii){
	var point = points[pii];
	var time = points[pii][0];
	var x = points[pii][1];
	x = parseFloat(x) - 2*gotime;
	if(x < -180){
		x += 360   
	}
	//****
	//****增加时间 time + points.length(s)
	//****
	points[pii][0] = addtime(time, 2*points.length);
	points[pii][1] = x +"";
	
	if(x == "-0"){
		x = 0;
	}
	
	var y = points[pii][2].substring(0,points[pii][2].indexOf("."));
	if(y == "-0"){
		y = 0;
	}			
	
	x = w + (parseInt(x)+180)*2;	
	y = h - (parseInt(y)+90)*2;
	document.getElementById("contener").innerHTML += "<div class=pointscss style='top: "+y+"px;left: "+x+"px;'></div>";		
}
//光照运动控制
function runmarquee(){
	var tab=document.getElementById("demo");
	var tab1=document.getElementById("demo1");
	var tab2=document.getElementById("demo2");
	tab2.innerHTML=tab1.innerHTML;
	var sp = sunTime/240;
	sp += "";
	if(sp.indexOf(".")!=-1){
		sp = sp.substring(0,sp.indexOf("."));
	}
	if(sp == "0"){
		sp = 0;
	}else{
		sp = parseInt(sp);
	}
	
	tab.scrollLeft+=sp;
	if(sp != 0){
		sunTime = sunTime%240;
		if(sunTime == 0){
			sunTime = 1;
		}
	}
}
//修正当前数据时是
function modifyTime(){
	var dateStr = formatDate(new Date());
	var point = points[count];
	var time = points[count][0];
	var beforetime = beforeThan(dateStr, time);
	//比较大小，修正值
	count += beforetime
	realPoint = count;	
}
//时间比较
function beforeThan(atime,btime){
	var ayear = 0;
	var amonth = 0;
	var adate = 0;
	var ahours = 0;
	var aminutes = 0;
	var asecond = 0;
	var amilliseconds = 0;
	var byear = 0;
	var bmonth = 0;
	var bdate = 0;
	var bhour = 0;
	var bminutes = 0;
	var bsecond = 0;
	var bmilliseconds = 0;
	
	if(atime instanceof Date){
		ayear = atime.getFullYear();
		amonth = atime.getMonth()+1;
		adate = atime.getDate();
		ahours = atime.getHours();
		aminutes = atime.getMinutes();
		asecond = atime.getSeconds();
		amilliseconds = time.getMilliseconds();
	}else{
		ayear = atime.substring(0,4);
		amonth = atime.substring(5,7);
		adate = atime.substring(8,10);
		ahours = atime.substring(11,13);
		aminutes = atime.substring(14,16);
		asecond = atime.substring(17,19);
		if(atime.length == 23){
			amilliseconds = atime.substring(20,23);
		}
	}
	if(btime instanceof Date){
		byear = btime.getFullYear();
		bmonth = btime.getMonth()+1;
		bdate = btime.getDate();
		bhours = btime.getHours();
		bminutes = btime.getMinutes();
		bsecond = btime.getSeconds();
		bmilliseconds = btime.getMilliseconds();
	}else{
		byear = btime.substring(0,4);
		bmonth = btime.substring(5,7);
		bdate = btime.substring(8,10);
		bhours = btime.substring(11,13);
		bminutes = btime.substring(14,16);
		bsecond = btime.substring(17,19);
		if(atime.length == 23){
			bmilliseconds = btime.substring(20,23);
		}
	}
	var ty = ayear - byear;
	var tm = amonth - bmonth;
	var td = adate - bdate;
	var th = ahours - bhours;
	var tmi = aminutes - bminutes;
	var ts = asecond - bsecond;
	var tms = amilliseconds - bmilliseconds;
	
	return ty*31536000 + tm*2592000 + td*86400 + th*3600 + tmi*60 + ts + tms/1000;
	
}
//加速控制
function setSpeed(isInTime){
	if(isInTime){
		for(var pi=0;pi<points.length;pi++){
			var point = points[pi];
			var time = points[pi][0];
			var x = points[pi][1];
			for(var ii=0;ii<addspeedshaptimes;ii++){
				x = parseFloat(x) + gotime;
				if(x > 180){
					x += -360   
				}
			}
			points[pi][1] = x +"";
			//****
			//****减去时间 time - shaptimes*points.length(s)
			//****
			points[pi][0] = addtime(time, -addspeedshaptimes*points.length);
		}	
		addspeedshaptimes = 0;
		speed = 1;
		modifyTime();
		sunTime = 1;
		setLightArea();
		var x = points[count][1].substring(0,points[count][1].indexOf("."));
		var y = points[count][2].substring(0,points[count][2].indexOf("."));
		if(x == "-0"){
			x = 0;
		}	
		if(y == "-0"){
			y = 0;
		}
		printAroundPionts(x,y)
		printPoint(count);
		document.getElementById("isintimeruning").innerHTML="实时状态";
	}else{
		var tempSpeed = document.getElementById("speed").value;
		speed = parseInt(tempSpeed);
		document.getElementById("isintimeruning").innerHTML="加速度状态";
	}
	document.getElementById("speed").value =speed;
	document.getElementById("speedshow").innerHTML=speed;
}


//卫星名称
function printAroundPionts(_x,_y){
	//x
	var x = parseInt(_x);
	var y = parseInt(_y);
	//显示卫星名字
	x = w + (parseInt(x)+180)*2 - 5;	
	y = h - (parseInt(y)+90)*2 + 4;
	document.getElementById("nowpoint").style.top = y;
	document.getElementById("nowpoint").style.left = x;
	document.getElementById("satellitenamediv").style.top = y+11 ;
	document.getElementById("satellitenamediv").style.left = x+5;
}


//时期格式化time==日期对象Date，type要格式化的样式
function formatDate(time,type){
	var format  = type || "yyyy-mm-dd HH:MM:SS.000";
	var year = time.getFullYear();
	var month = time.getMonth()+1;
	var date = time.getDate();
	var hours = time.getHours();
	var minutes =time.getMinutes();
	var second = time.getSeconds();
	var milliseconds = time.getMilliseconds();
	if(format.indexOf("yyyy")!=-1){
		format = format.replace("yyyy", year);
	}
	if(format.indexOf("mm")!=-1){
		if(month<10){
			month = "0" + month;
		}
		format = format.replace("mm", month);
	}
	if(format.indexOf("dd")!=-1){
		if(date<10){
			date = "0" + date;
		}
		format = format.replace("dd", date);
	}
	if(format.indexOf("HH")!=-1){
		if(hours<10){
			hours = "0" + hours;
		}
		format = format.replace("HH", hours);
	}
	if(format.indexOf("MM")!=-1){
		if(minutes<10){
			minutes = "0" + minutes;
		}
		format = format.replace("MM", minutes);
	}
	if(format.indexOf("SS")!=-1){
		if(second<10){
			second = "0" + second;
		}
		format = format.replace("SS",second);
	}
	if(format.indexOf("000")!=-1){
		format = format.replace("ccc",milliseconds);
	}
	return format;
}
//加减时间
function addtime(time, sec){
	var date = new Date();
	date.setYear(parseInt(time.substring(0, 4)));
	date.setMonth(parseInt(time.substring(5, 7)));
	date.setDate(parseInt(time.substring(8, 10)));
	var hs = time.substring(11, 13);
	if(hs.indexOf("0") == 0){
		hs = hs.substring(1);
	}
	date.setHours(parseInt(hs));
	var Ms =time.substring(14, 16);
	if(Ms.indexOf("0") == 0){
		Ms = Ms.substring(1);
	}
	date.setMinutes(parseInt(Ms));
	var Ss = time.substring(17, 19);
	if(Ss.indexOf("0") == 0){
		Ss = Ss.substring(1);
	}	
	date.setSeconds(parseInt(Ss));
	
	if(sec instanceof String){
		sec = parseInt(sec);
	}
	date.setSeconds(date.getSeconds() + sec);
	date.setMonth(date.getMonth()-1);
	return formatDate(date);
}

